package com.hkzhao.reportingsystem.dto;

import com.hkzhao.reportingsystem.mbg.model.Adminuser;
import com.hkzhao.reportingsystem.mbg.model.UserPermission;
import com.hkzhao.reportingsystem.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class AdminUserDetail implements UserDetails {
    @Autowired(required = false)
    private AdminUserService adminUserService;
    private Adminuser adminuser;
    private String password;
    private List<UserPermission> permissionList;
    public AdminUserDetail(Adminuser adminuser,List<UserPermission> permissions,String password){
        this.adminuser = adminuser;
        this.permissionList = permissions;
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissionList.stream().filter(userPermission -> userPermission.getValue()!=null)
                .map(userPermission -> new SimpleGrantedAuthority(userPermission.getValue()))
                .collect(Collectors.toList());
    }
    public String getPassword(){
        return password;
    }
    public String getUsername(){
        return adminuser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
}
